package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: rwalters
 * Date: 31/01/13
 * Time: 17:11
 * To change this template use File | Settings | File Templates.
 */
public class Biblioteca {
    public Biblioteca(InOut _inOut) {
        inOut = _inOut;
        homeMenu = new Menu(inOut);
        libraryMenu = new Menu(inOut);
        movieMenu = new MovieMenu(inOut);
        membershipMenu = new Menu(inOut);
        bookMenu = new Menu(inOut);
        login = new Login(inOut);
        bookLibrary.create();
        movieLibrary.create();
        createHomeMenu();
        createLibraryMenu();
        createMovieMenu();
        createMembershipMenu();
        createLoginScreen();
    }

    InOut inOut;
    Menu homeMenu;
    Menu libraryMenu;
    MovieMenu movieMenu;
    Menu membershipMenu;
    Login login;
    Menu bookMenu;
    BookLibrary bookLibrary = new BookLibrary();
    MovieLibrary movieLibrary = new MovieLibrary();

    public void start() {
        displayWelcomeMessage();
        displayAndReadHomeMenu();
    }

    public void displayWelcomeMessage() {
        inOut.println("Welcome to Biblioteca!");
    }

    public void displayAndReadHomeMenu() {
        if (login.loggedIn) {
            homeMenu.itemList.get(4).text = "[4] Logout";
            homeMenu.itemList.get(4).select =  new MenuCallback() {
                public void Invoke() {
                    login.loggedIn = false;
                    inOut.println("You have successfully logged out.");
                    displayAndReadHomeMenu();
                }
            };
        }
        else {
            homeMenu.itemList.get(4).text = "[4] Login";
            homeMenu.itemList.get(4).select =  new MenuCallback() {
                public void Invoke() {  login.start(); } };
        }
        homeMenu.displayAndRead();
    }

    public void displayAndReadMembershipMenu() {
        if (login.loggedIn) {
            membershipMenu.title = login.currentUser.name + ", " + login.currentUser.email + ", " + login.currentUser.phone;
        }
        else {
            membershipMenu.title = "Please talk to a Librarian. Thank you.";
        }
        membershipMenu.displayAndRead();
    }

    public void createHomeMenu() {
        homeMenu.title = "Please choose from the following menu options:";
        homeMenu.itemList = new ArrayList<MenuItem>();
        homeMenu.itemList.add(new MenuItem("[0] Exit application", new MenuCallback() {
            public void Invoke() { return; } }));
        homeMenu.itemList.add(new MenuItem("[1] View all books", new MenuCallback() {
            public void Invoke() { libraryMenu.displayAndRead(); } }));
        homeMenu.itemList.add(new MenuItem("[2] View all movies", new MenuCallback() {
            public void Invoke() { movieMenu.displayAndRead(); } }));
        homeMenu.itemList.add(new MenuItem("[3] Check membership details", new MenuCallback() {
            public void Invoke() {  displayAndReadMembershipMenu(); } }));
        homeMenu.itemList.add(new MenuItem("[4] Login", new MenuCallback() {
            public void Invoke() {  login.start(); } }));
    }

    public void createLibraryMenu() {
        libraryMenu.title = "LIBRARY";
        libraryMenu.itemList = new ArrayList<MenuItem>();
        libraryMenu.itemList.add(new MenuItem("[0] Previous menu", new MenuCallback() {
            public void Invoke() { displayAndReadHomeMenu(); } }));
        addBooksToLibraryMenu();
    }

    public void createMembershipMenu() {
        membershipMenu.title = "Please talk to a Librarian. Thank you.";
        membershipMenu.itemList = new ArrayList<MenuItem>();
        membershipMenu.itemList.add(new MenuItem("[0] Previous menu", new MenuCallback() {
            public void Invoke() { displayAndReadHomeMenu(); } }));
    }

    public void createBookMenu(Book book) {
        final Book _book = book;
        bookMenu.title = book.title;
        bookMenu.itemList = new ArrayList<MenuItem>();
        bookMenu.itemList.add(new MenuItem("[0] Previous menu", new MenuCallback() {
            public void Invoke() { libraryMenu.displayAndRead(); } }));
        if (login.loggedIn) {
            bookMenu.itemList.add(new MenuItem("[1] Reserve book", new MenuCallback()
            {
                public void Invoke()
                {
                    _book.reserve(inOut);
                    libraryMenu.displayAndRead();
                }
            }));
        }
    }

    public void createMovieMenu() {
        movieMenu.title = "MOVIES";
        movieMenu.header = "Movie, Year, Director, Rating";
        movieMenu.itemList = new ArrayList<MenuItem>();
        movieMenu.itemList.add(new MenuItem("[0] Previous menu", new MenuCallback() {
            public void Invoke() { displayAndReadHomeMenu(); } }));
        addmoviesToMovieMenu();
    }

    public void createLoginScreen() {
        login.menuCallback = new MenuCallback() {
            public void Invoke() { displayAndReadHomeMenu(); } };
        login.users = getUsers();
    }

    private void addBooksToLibraryMenu() {
        for (Book book : bookLibrary) {
            libraryMenu.itemList.add(new MenuItem());
            MenuItem item = libraryMenu.itemList.get(libraryMenu.itemList.size() - 1);
            final Book _book = book;
            item.text = "[" + (libraryMenu.itemList.size() - 1) + "] " + _book.title;
            item.select = new MenuCallback()
            {
                public void Invoke()
                {
                    createBookMenu(_book);
                    bookMenu.displayAndRead();
                }
            };
        }
    }

    private void addmoviesToMovieMenu() {
        for (Movie movie : movieLibrary) {
            movieMenu.itemList.add(new MenuItem());
            MenuItem item = movieMenu.itemList.get(movieMenu.itemList.size() - 1);
            item.text = movie.title + ", " + movie.year + ", " + movie.director + ", " + movie.ratingToString();
            item.select = new MenuCallback()
            {
                public void Invoke()
                { return;
                }
            };
        }
    }

    private List<User> getUsers() {
        List<User> users = new ArrayList<User>();
        for (int n = 0; n < 2; n++) {
            User user = new User();
            user.username = "111-111" + (n + 1);
            user.password = "password";
            user.name = "Joe Bloggs " + (n + 1);
            user.email = "jbloggs@outlook.com";
            user.phone = "01234567890";
            users.add(user);
        }
        return users;
    }
}
